const app = getApp()
const db = wx.cloud.database();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        jcjl: "",
        currentData: 0,
        windowHeight: "",
        admin_address: "广东省深圳市坪山区"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //获取用户屏幕高度
        wx.getSystemInfo({
            success: (res) => {
                let windowHeight = res.windowHeight
                this.setData({
                    windowHeight: windowHeight
                })
            }
        })
        //根据身份证号读取用户数据库中检测记录
        let phonenum = app.globalData.phonenum
        db.collection('gift').orderBy('time', 'desc').get({
            success: res => {
                this.setData({
                    jcjl: res.data
                })
            }
        })
    },

    //切换检测完成/检测中状态页
    bindchange: function (e) {
        const that = this;
        that.setData({
            currentData: e.detail.current
        })
    },
    //点击切换，滑块index赋值
    checkCurrent: function (e) {
        const that = this;
        if (that.data.currentData === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentData: e.target.dataset.current
            })
        }
    },

    //点击查看详情时，获取点击项的id，用以生成具体信息
    onclick: function (e) {
        console.log("e.currentTarget.dataset.text", e.currentTarget.dataset.text);
        app.globalData.id = e.currentTarget.dataset.text;
    },
    Download() {
        let userdata = this.data.jcjl
        let csv = '编号,用户地址,用户回寄快递单号,完成状态,用户姓名,收件人姓名,用户手机号,收件人手机号,性别,申请时间,样品寄出时间,样品回寄时间,用户回寄快递单号,用户备注\n'
        for (var i in userdata) {
            // console.log(userdata[i].valueOf())
            for (var key in userdata[i]) {
                csv += userdata[i][key] + ','
            }
            csv += '\n'
        }
        //将csv写入样本数据.csv文件
        // 引入fs模块
        const fs = wx.getFileSystemManager()
        fs.writeFile({
            filePath: `${wx.env.USER_DATA_PATH}/样本数据.csv`,
            data: csv,
            encoding: 'utf8',
            success(res) {
                console.log(res)
                wx.shareFileMessage({
                    filePath: `${wx.env.USER_DATA_PATH}/样本数据.csv`,
                    success(res) {
                        console.log(res)
                    },
                    fail(res) {
                        console.log(res)
                    }
                })
            },
            fail(res) {
                console.error(res)
            }
        })
    }
})